
from flask import Blueprint, request, jsonify
from models import db, College, Course, Teacher

colleges_bp = Blueprint('colleges', __name__)

# 获取学院数据
@colleges_bp.route('/api/colleges', methods=['GET'])
def get_colleges():
    colleges = College.query.all()
    result = [{
        'college_id': college.college_id,
        'college_name': college.college_name,
        'course_count': len(Course.query.filter_by(college_id=college.college_id).all()),
        'teacher_count': len(Teacher.query.filter_by(college_id=college.college_id).all())
    } for college in colleges]
    return jsonify(result)

# 添加学院
@colleges_bp.route('/api/colleges', methods=['POST'])
def add_college():
    data = request.get_json()
    new_college = College(college_name=data['college_name'])
    db.session.add(new_college)
    db.session.commit()
    return jsonify({'college_id': new_college.college_id, 'college_name': new_college.college_name}), 201
